package com.christian.thread.visibility;

import sun.misc.Unsafe;

import java.util.concurrent.TimeUnit;

/**
 * 文件描述：多线程的之间操作共享变量的可见性
 *
 * @Author：wang
 * @Date：2020-01-15 20:21:35
 */
public class VolatialCase {

    private volatile boolean flag = true;

    public void method() {
        System.out.println("方法开始执行！");
        while (flag) {

        }

        System.out.println("方法执行完成！");
    }

    public static void main(String[] args) {
        VolatialCase volatialCase = new VolatialCase();

        new Thread(volatialCase::method, "Thread1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        volatialCase.flag = false;
    }
}
